<?php

namespace Home\Controller;
use Home\Common\Controller\CommonController;

class LgController extends CommonController
{
    protected  static $referer;
    /**
     * 登录
     */
   public function login(){

       if(!isset($_COOKIE['referer'])){
//           dd($_SERVER['HTTP_REFERER']);die;
           cookie('referer',$_SERVER['HTTP_REFERER']);
       }
       $gid=I('get.gid');
       if(IS_POST){
           //非空验证
           if(notNull(I('post.uname')||I('post.upass'))){
               $this->error('用户名或密码不能为空');die;
           }
           //验证验证码
           if(!$this->check_verify(I('post.code'))){
               $this->error('验证码不正确');die;
           }
           $uname=I('post.uname');
             $uname=addslashes($uname);
           $upass=md5($_POST['upass']);

//           dd($upass);
//           dd(M('user')->where("uname='$uname'")->getField('upass'));die;
           if(!M('user')->where("uname='$uname'")->find()){
               $this->error('用户名不存在');die;
           }
           if(M('user')->where("uname='$uname'")->find() && M('user')->where("uname='$uname'")->getField('upass')==$upass ){
//                 dd("登录成功");die;
               $data=M('user')->where("uname='$uname'")->find();
               session('uid',$data['uid']);
               session('uname',$uname);
//               dd($data);
               if($gid){
                  //表示是点击购物车进来的
                   //向商品信息表中追加数据
                   $info=[$data['uid'],$gid];
                   $res=D('usergoods')->store($info);
                   $this->success('登录成功',U('home/car/lists',['uid'=>$data['uid']]));die;
               }else{
                  //表示直接登录进来的
                   $this->redirect('home/index/index');die;
//                   $url=cookie('referer');
//                 header("location:$url");die;
               }
           }else{
               $this->error('密码错误');die;
           }
       }
       $this->display();
   }
    /**
     * 注册
     */
    public function regest(){
           if(IS_POST){
               //非空验证
               if(notNull(I('post.uname')||I('post.upass'))){
                  $this->error('用户名或密码不能为空');
               }
               //验证验证码
               if(!$this->check_verify(I('post.code'))){
                   $this->error('验证码不正确');
               }
               $_POST['utime']=time();
//               dd($_POST['upass']);die;
               $_POST['upass']=md5($_POST['upass']);
              $res=D('user')->regest($_POST);
                 D('userinfo')->addinfo($res);
               if($res){
                   $this->success('注册成功,请登录',U('home/lg/login'));die;
               }else{
                   $this->error('注册失败');die;
               }
           }
        $this->display();
    }

    /**
     * 登录验证码
     */
    public function logincode(){
        $config =    array(
            'fontSize'   =>    30,    // 验证码字体大小
            'length'     =>    4,     // 验证码位数
            'useNoise'   =>    true, // 关闭验证码杂点
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }

    /**
     * 注册验证码
     */
    public function code(){
        $config =    array(
            'fontSize'   =>    30,    // 验证码字体大小
            'length'     =>    4,     // 验证码位数
            'useNoise'   =>    true, // 关闭验证码杂点
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }
    /**
     * 验证码验证
     */
    private  function check_verify($code, $id = ''){
       $verify = new \Think\Verify();
       return $verify->check($code, $id);
     }
    /**
     * 退出登录
     */
    public function exitlogin(){
        session(null);
        session('[destroy]');
        $this->redirect('home/index/index');die;
    }
}